package com.achobeta.recruitment.service.impl.interact;

import com.achobeta.recruitment.common.ResultCode;
import com.achobeta.recruitment.dao.interact.FeedbackMapper;
import com.achobeta.recruitment.model.Feedback;
import com.achobeta.recruitment.model.Message;
import com.achobeta.recruitment.model.vo.FeedbackVO;
import com.achobeta.recruitment.service.interact.FeedBackService;
import com.achobeta.recruitment.service.interact.MessageService;
import com.achobeta.recruitment.utils.CheckUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-31
 * Time: 23:02
 */
@Slf4j
@Service
public class FeedBackServiceImpl implements FeedBackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private MessageService messageService;

    @Override
    public void submitFeedBack(Integer messageId, String openid, String content) {
        // 校验
        CheckUtils.checkNULL(messageId);
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_VALIDATE, openid, content);
        // 查询消息
        Message message = messageService.selectMessageById(messageId);
        // 校验
        if(message == null || message.getDeleteState() != 0) {
            CheckUtils.printWarn(ResultCode.FAILED_MESSAGE_IS_EXISTS);
        }
        if(message.getState() != 1) {
            CheckUtils.printWarn(ResultCode.FAILED_UNAUTHORIZED);
        }
        if(!openid.equals(message.getOpenid())) {
            CheckUtils.printWarn(ResultCode.FAILED_UNAUTHORIZED);//没有权限看到详情
        }
        // 构造反馈
        Feedback feedback = new Feedback();
        feedback.setContent(content);
        feedback.setOpenid(openid);
        feedback.setState((byte) 0);//未读
        feedback.setMessageId(messageId);
        feedback.setDeleteState((byte) 0);
        Date date = new Date();
        feedback.setCreateTime(date);
        feedback.setUpdateTime(date);
        // 插入数据库
        int rows = feedbackMapper.insertSelective(feedback);
        // 校验
        CheckUtils.checkRows(rows, 1, ResultCode.FAILED_CREATE);
        // 更新消息的状态为已反馈，也就是2
        messageService.updateState(messageId, (byte) 2);
        // 成功
        log.info(ResultCode.SUCCESS + "  反馈id：" + feedback.getId());
    }

    @Override
    public Long getNotAwareNumber() {
        Long count = feedbackMapper.getNotAwareNumber();
        log.info(ResultCode.SUCCESS + "  未读反馈数： " + count);
        return count;
    }

    @Override
    public List<FeedbackVO> getAllFeedback() {
        List<FeedbackVO> feedbackVOS = feedbackMapper.getAllFeedback();
        feedbackVOS = feedbackVOS == null ? new ArrayList<>() : feedbackVOS;
        log.info(ResultCode.SUCCESS + "  查询到的行数： " + feedbackVOS.size());
        return feedbackVOS;
    }

    @Override
    public Feedback getFeedbackById(Integer id) {
        CheckUtils.checkNULL(id);
        return feedbackMapper.selectByPrimaryKey(id);//无论如何都返回
    }

    @Override
    public void remarkRead(Integer id) {
        // 校验
        CheckUtils.checkNULL(id);
        // 反馈必须存在
        Feedback feedback = feedbackMapper.selectByPrimaryKey(id);
        CheckUtils.checkNULL(feedback);
        // 构造修改用的对象
        Feedback updateFeedback = new Feedback();
        updateFeedback.setId(id);
        updateFeedback.setState((byte) 1);
        updateFeedback.setUpdateTime(new Date());//即使状态本就为1，这里也不同，为最近被读的时间
        // 调用dao层
        int rows = feedbackMapper.updateByPrimaryKeySelective(updateFeedback);
        CheckUtils.checkRows(rows, 1, ResultCode.FAILED_FEEDBACK_STATE_UPDATE);
        // 成功
        log.info(ResultCode.SUCCESS + " 被读的反馈： " + id);
    }
}
